package org.jeecg.modules.quartz.job;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.RandomUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.engineer.entity.MtEngineer;
import org.jeecg.modules.engineer.service.IMtEngineerService;
import org.jeecg.modules.order.entity.MtRepairDispatch;
import org.jeecg.modules.order.entity.MtRepairOrder;
import org.jeecg.modules.order.entity.MtRepairSchedule;
import org.jeecg.modules.order.service.IMtRepairOrderService;
import org.jeecg.wechat.WeChatApi;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
public class SendOrdersJob implements Job {

    @Autowired
    private IMtRepairOrderService mtRepairOrderService;
    @Autowired
    private IMtEngineerService mtEngineerService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(" SendOrdersJob Execution key：" + jobExecutionContext.getJobDetail().getKey());
        //定时任务实现自动派单
        List<MtRepairOrder> repairOrderList = mtRepairOrderService.list(new LambdaQueryWrapper<MtRepairOrder>()
                .eq(MtRepairOrder::getProcessingStatus, "0"));
        for (MtRepairOrder mtRepairOrder : repairOrderList) {
            if (Objects.isNull(mtRepairOrder)) {
                continue;
            }
            List<MtEngineer> engineerList = mtEngineerService.list(new LambdaQueryWrapper<MtEngineer>()
                    .eq(MtEngineer::getStationId, mtRepairOrder.getStationId()));
            if (engineerList.isEmpty()) {
                continue;
            }
            // 筛选出工单数最少的工程师
            for (MtEngineer engineer : engineerList) {
                engineer.setOrderNum(mtRepairOrderService.getProcessingListByEngineer(engineer.getId()).size());
            }
            Integer minNum = engineerList.stream()
                    .map(MtEngineer::getOrderNum)
                    .min(Integer::compareTo).orElse(0);
            List<MtEngineer> dispatchEngineerList = engineerList.stream()
                    .filter(mtEngineer -> mtEngineer.getOrderNum().equals(minNum))
                    .collect(Collectors.toList());
            if (dispatchEngineerList.isEmpty()) {
                continue;
            }
            int number = RandomUtils.nextInt(dispatchEngineerList.size());
            MtEngineer dispatchEngineer = dispatchEngineerList.get(number);
            // 工单数据补充
            mtRepairOrder.setProcessingStatus("1")//已派工
                    .setSysOrgCode(dispatchEngineer.getSysOrgCode());//orgCode绑定

            // 工程师数据装配
            MtRepairDispatch mtRepairDispatch = new MtRepairDispatch();
            mtRepairDispatch.setEngineerId(dispatchEngineer.getId())//绑定工程师
                    .setProviderId(dispatchEngineer.getProviderId())//绑定服务商
                    .setStationId(dispatchEngineer.getStationId())//绑定服务站
                    .setDispatchTime(DateUtil.date())//派工时间
                    .setWorkStatus(0)//工作状态默认值：未响应
                    .setWorkType("负责");//人员类型：负责


            // 进度跟踪数据装配
            MtRepairSchedule mtRepairSchedule = new MtRepairSchedule();
            mtRepairSchedule.setScheduleStatus(1)//进度状态：派工
                    .setScheduleTime(DateUtil.date())//进度时间
                    .setEngnieerId(dispatchEngineer.getId());//工程师绑定
            // 进度描述装配
            mtRepairSchedule.setRemarks(mtRepairSchedule.getScheduleTime()
                    + "  编号:" + mtRepairOrder.getOrderNo() + "的工单 自动分配给："
                    + mtRepairDispatch.getWorkType() + "-" + dispatchEngineer.getEngineerName() + "。");

            mtRepairOrderService.repairOrderDispatch(mtRepairOrder, mtRepairDispatch, mtRepairSchedule);
        }
        log.info(String.format(" maintain-Boot 普通定时任务 SendOrdersJob !  时间:" + DateUtils.getTimestamp()));
    }
}
